package frame;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import Client.MyClient;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;

import javax.swing.ImageIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Set;

public class FindPassword extends JFrame {
	private MyClient myClient;
	private JPanel contentPane;
	private JTextField text_username;
	private JLabel ok;
	private JLabel lblNewLabel;
	private JLabel lb2_3;
	private JTextField text_question;
	private JTextField text_answer;
	private JLabel lb2_1;
	private JTextField text_password;
	private JLabel lb2_2;
	private JTextField text_next_password;
	private JLabel lb2_4;
	private JLabel font_start_1;
	private JLabel font_start_2;
	private JLabel font_start_3;
	private JLabel font_start_4;
	private JLabel bt_find;
	private String answringString;
	/**
	 * Launch the application.
	 */

	/**
	 * Create the frame.
	 */
	public void setQuestion(String string) {
		text_question.setText(string);
	}
	public void queryQuestion() {
		try {
			myClient.FindQuestion(text_username.getText(),this);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	public FindPassword(MyClient myClient) {
		this.myClient=myClient;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 806, 513);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel font_start = new JLabel("");
		ImageIcon imageIconSource2 = new ImageIcon("photo\\username.png");
	    Image image1 = imageIconSource2.getImage();
	    Image tempImage1 = image1.getScaledInstance(200,70,Image.SCALE_DEFAULT);
	    ImageIcon imageIcon1 = new ImageIcon(tempImage1);
		font_start.setIcon(imageIcon1);
		font_start.setBounds(58, 3, 207, 57);
		contentPane.add(font_start);
		
		JLabel lb2 = new JLabel("");
		lb2.setForeground(Color.WHITE);
		lb2.setFont(new Font("宋体", Font.BOLD, 20));
		ImageIcon imageIconSource1 = new ImageIcon("photo\\input.png");
	     image1 = imageIconSource1.getImage();
	     tempImage1 = image1.getScaledInstance(250,70,Image.SCALE_DEFAULT);
	    imageIcon1 = new ImageIcon(tempImage1);
	    
		ImageIcon imageIconSource0 = new ImageIcon("photo\\fined.png");
	    Image image0 = imageIconSource0.getImage();
	    Image tempImage0 = image0.getScaledInstance(70,70,Image.SCALE_DEFAULT);
	    ImageIcon imageIcon0 = new ImageIcon(tempImage0);
	    
	    bt_find = new JLabel("");
	    bt_find.addMouseListener(new MouseAdapter() {
	    	@Override
	    	public void mouseClicked(MouseEvent e) {
	    		//查询username
	    		queryQuestion();
	    		//查找问题，给text_question赋值
	    	}
	    });
	    bt_find.setIcon(imageIcon0);
	    bt_find.setBounds(647, 13, 81, 68);
	    contentPane.add(bt_find);
	    
	    ok = new JLabel("");
	    ok.addMouseListener(new MouseAdapter() {
	    	@Override
	    	public void mouseClicked(MouseEvent e) {
	    		if(text_password.getText().equals(new String(text_next_password.getText()))) {
					String ansString=text_answer.getText();
					if(getAnswringString().equals(ansString)) {
						myClient.ResetPassword(text_username.getText(),text_password.getText());
					}else {
						JOptionPane.showMessageDialog(FindPassword.this, "密保问题回答错误");
					}
				}else {
					JOptionPane.showMessageDialog(FindPassword.this, "密码不一致请检查");
				}
	    		//查询数据库
	    			
	    		//找回密码
	    	}
	    	@Override
	    	public void mouseEntered(MouseEvent e) {
	    		 ok.setIcon(new ImageIcon("photo//font_start2.png"));
	    	}
	    	@Override
	    	public void mouseExited(MouseEvent e) {
	    		 ok.setIcon(new ImageIcon("photo//font_start1.png"));
	    	}
	    });
	    ok.setIcon(new ImageIcon("photo//font_start1.png"));
	    ok.setBounds(245, 375, 207, 47);
	    contentPane.add(ok);
	    
	    text_username = new JTextField();
	    text_username.setFont(new Font("宋体", Font.BOLD, 20));
	    text_username.setBackground(Color.DARK_GRAY);
	    text_username.setOpaque(false);
	    text_username.setForeground(Color.WHITE);
	    text_username.setBounds(368, 23, 207, 30);
	    contentPane.add(text_username);
	    text_username.setColumns(10);
	    lb2.setIcon(imageIcon1);
	    lb2.setOpaque(false);
		lb2.setBounds(343, 13, 254, 47);
		contentPane.add(lb2);
		ImageIcon imageIconSource21 = new ImageIcon("photo\\start.jpg");
	    Image image2 = imageIconSource21.getImage();
	    Image tempImage2 = image2.getScaledInstance(900,600,Image.SCALE_DEFAULT);
	    ImageIcon imageIcon2 = new ImageIcon(tempImage2);
	    lb2.setOpaque(false);
		
		text_question = new JTextField();
		text_question.setOpaque(false);
		text_question.setForeground(Color.WHITE);
		text_question.setFont(new Font("宋体", Font.BOLD, 20));
		text_question.setColumns(10);
		text_question.setBackground(Color.DARK_GRAY);
		text_question.setBounds(368, 92, 207, 30);
		contentPane.add(text_question);
		
		lb2_3 = new JLabel("");
		lb2_3.setOpaque(false);
		lb2_3.setIcon(imageIcon1);
		lb2_3.setForeground(Color.WHITE);
		lb2_3.setFont(new Font("宋体", Font.BOLD, 20));
		lb2_3.setBounds(343, 82, 254, 47);
		contentPane.add(lb2_3);
		
		text_answer = new JTextField();
		text_answer.setOpaque(false);
		text_answer.setForeground(Color.WHITE);
		text_answer.setFont(new Font("宋体", Font.BOLD, 20));
		text_answer.setColumns(10);
		text_answer.setBackground(Color.DARK_GRAY);
		text_answer.setBounds(368, 164, 207, 30);
		contentPane.add(text_answer);
		
		lb2_1 = new JLabel("");
		lb2_1.setOpaque(false);
		lb2_1.setForeground(Color.WHITE);
		lb2_1.setIcon(imageIcon1);
		lb2_1.setFont(new Font("宋体", Font.BOLD, 20));
		lb2_1.setBounds(343, 154, 254, 47);
		contentPane.add(lb2_1);
		
		text_password = new JTextField();
		text_password.setOpaque(false);
		text_password.setForeground(Color.WHITE);
		text_password.setFont(new Font("宋体", Font.BOLD, 20));
		text_password.setColumns(10);
		text_password.setBackground(Color.DARK_GRAY);
		text_password.setBounds(368, 229, 207, 30);
		contentPane.add(text_password);
		
		lb2_2 = new JLabel("");
		lb2_2.setOpaque(false);
		lb2_2.setForeground(Color.WHITE);
		lb2_2.setIcon(imageIcon1);
		lb2_2.setFont(new Font("宋体", Font.BOLD, 20));
		lb2_2.setBounds(343, 219, 254, 47);
		contentPane.add(lb2_2);
		
		text_next_password = new JTextField();
		text_next_password.setOpaque(false);
		text_next_password.setForeground(Color.WHITE);
		text_next_password.setFont(new Font("宋体", Font.BOLD, 20));
		text_next_password.setColumns(10);
		text_next_password.setBackground(Color.DARK_GRAY);
		text_next_password.setBounds(368, 296, 207, 30);
		contentPane.add(text_next_password);
		
		lb2_4 = new JLabel("");
		lb2_4.setOpaque(false);
		lb2_4.setForeground(Color.WHITE);
		lb2_4.setIcon(imageIcon1);
		lb2_4.setFont(new Font("宋体", Font.BOLD, 20));
		lb2_4.setBounds(343, 286, 254, 47);
		contentPane.add(lb2_4);
		
		ImageIcon imageIconSource3 = new ImageIcon("photo\\question1.png");
	    Image image3 = imageIconSource3.getImage();
	    Image tempImage3 = image3.getScaledInstance(200,70,Image.SCALE_DEFAULT);
	    ImageIcon imageIcon3 = new ImageIcon(tempImage3);
		font_start_1 = new JLabel("");
		font_start_1.setIcon(imageIcon3);
		font_start_1.setBounds(58, 72, 207, 57);
		contentPane.add(font_start_1);
		
		ImageIcon imageIconSource4 = new ImageIcon("photo\\answer1.png");
	    Image image4 = imageIconSource4.getImage();
	    Image tempImage4 = image4.getScaledInstance(200,70,Image.SCALE_DEFAULT);
	    ImageIcon imageIcon4 = new ImageIcon(tempImage4);
		font_start_2 = new JLabel("");
		font_start_2.setIcon(imageIcon4);
		font_start_2.setBounds(58, 144, 207, 57);
		contentPane.add(font_start_2);
		
		ImageIcon imageIconSource5 = new ImageIcon("photo\\password.png");
	    Image image5 = imageIconSource5.getImage();
	    Image tempImage5 = image5.getScaledInstance(200,70,Image.SCALE_DEFAULT);
	    ImageIcon imageIcon5 = new ImageIcon(tempImage5);
	    
		font_start_3 = new JLabel("");
		font_start_3.setIcon(imageIcon5);
		font_start_3.setBounds(58, 216, 207, 57);
		contentPane.add(font_start_3);
		

		ImageIcon imageIconSource6 = new ImageIcon("photo\\two_password.png");
	    Image image6 = imageIconSource6.getImage();
	    Image tempImage6 = image6.getScaledInstance(220,70,Image.SCALE_DEFAULT);
	    ImageIcon imageIcon6 = new ImageIcon(tempImage6);
	    
		font_start_4 = new JLabel("");
		font_start_4.setIcon(imageIcon6);
		font_start_4.setBounds(58, 286, 207, 57);
		contentPane.add(font_start_4);
		

		
		lblNewLabel = new JLabel("");
		lblNewLabel.setBounds(0, 0, 798, 500);
		lblNewLabel.setIcon(imageIcon2);
		contentPane.add(lblNewLabel);
	}
	public String getAnswringString() {
		return answringString;
	}
	public void setAnswringString(String answringString) {
		this.answringString = answringString;
	}
	public void isReset(boolean a) {
		if(a) {
			JOptionPane.showMessageDialog(FindPassword.this, "修改密码成功");
			this.dispose();
		}else {
			JOptionPane.showMessageDialog(FindPassword.this, "修改密码失败");
		}
	}

}
